layui.use(['table', 'treetable','miniPage','element'], function () {
    var $ = layui.jquery;
    var table = layui.table;
    var treetable = layui.treetable,
    miniPage = layui.miniPage;

    var laodTable = function (){
        layer.load(2);
        treetable.render({
            treeColIndex: 1,
            treeSpid: 0,
            treeIdName: 'id',
            treePidName: 'pid',
            elem: '#menu-table',
            url: 'menu/all',
            toolbar: '#toolbarDemo',
            page: false,
            cols: [[
                {type: 'numbers'},
                {field: 'name', minWidth: 200, title: '菜单名字'},
                // {field: 'name', title: '菜单名字'},
                {field: 'icon', title: '图标'},
                {field: 'href', title: '菜单链接'},
                {
                    field: 'href', width: 80, align: 'center', templet: function (d) {
                        //判断href是否为空
                        if (isEmpty(d.href)) {
                            return '<span class="layui-badge layui-bg-gray">目录</span>';
                        } else {
                            return '<span class="layui-badge-rim">菜单</span>';
                        }
                    }, title: '类型'
                },
                {templet: '#auth-state', width: 120, align: 'center', title: '操作'}
            ]],
            done: function () {
                layer.closeAll('loading');
            }
        });
    }    // 渲染表格
    laodTable();
    $('#btn-expand').click(function () {
        treetable.expandAll('#menu-table');
    });

    $('#btn-fold').click(function () {
        treetable.foldAll('#menu-table');
    });

    table.on('toolbar(menu-table)', function (obj) {
        if (obj.event === 'add') {   // 监听添加操作
            var content = miniPage.getHrefContent('page/business/menuAdd.html');
            var openWH = miniPage.getOpenWidthHeight();

            var index = layer.open({
                title: '增加菜单',
                type: 1,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: [openWH[0]*0.8 + 'px', openWH[1] + 'px'],
                offset:'auto',
                content: content,
                end:function (){
                    console.log("弹出窗被销毁了")
                    laodTable()
                }
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
        } else if (obj.event === 'delete') {  // 监听删除操作
            var checkStatus = table.checkStatus('currentTableId')
                , data = checkStatus.data;
            layer.alert(JSON.stringify(data));
        }
    });

    //监听工具条
    table.on('tool(menu-table)', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'del') {
            // layer.msg('删除' + data.id);
            if (!isEmpty(data.id)){
                $.ajax({
                    type: "GET",
                    url: "menu/del/"+data.id,//发送请求
                    success: function (result) {
                        if (result.code == 0){
                            layer.msg(result.msg)
                            laodTable()
                        }else {
                            layer.msg(result.msg)
                        }
                    },
                    error: function (err) {
                       console.log(err)
                    }
                })
            }

        } else if (layEvent === 'edit') {
            layer.msg('修改' + data.id);
        }
    });
});

function isEmpty(obj) {
    if(typeof obj == "undefined" || obj == null || obj == ""){
        return true;
    }else{
        return false;
    }
}
